<?php 

class FRM_View_Ext
{
	/**
	 * Enter description here...
	 *
	 * @var FRM_Db_Table
	 */
	protected $model;
	
	protected $langs = array();

	protected $localizedFields = array();

	protected $fields = array();
	
	protected $windowFormWidth = 700;
	
	protected $module;
	
	protected $controller;
	protected $action;
	
	public function __construct($model, $jsDir, $module, $controller, $action, $windowFormWidth){
		$this->model = $model;
		
		if(!is_null($this->model)){
			$this->fields = $this->model->getFields() ;

			if(method_exists($this->model, 'getLocalizedFields')){
				$this->localizedFields = $this->model->getLocalizedFields();
			}

			if(is_null($this->localizedFields)){
				$this->localizedFields = array();
			}

			if(method_exists($this->model, 'getLangs')){
				$this->langs = $this->model->getLangs();
			}

			if(is_null($this->langs)){
				$this->langs = array();
			}

		}
		
		
		$this->windowFormWidth = $windowFormWidth;
		FRM_View_Ext_Abstract::SetJs($jsDir);
		FRM_View_Ext_Abstract::SetModule($module);
		$this->module = $module;
		FRM_View_Ext_Abstract::SetController($controller);
		$this->controller = $controller;
		$this->action = $action;
		FRM_View_Ext_Abstract::setLangs($this->langs);
	}

	public function renderWindow($method = 'grid'){
		if(!is_null($this->model)){

			$this->renderGrid($method);


			//Создаем екземпляр формы
			$form = new FRM_View_Ext_Form();
			$searchForm = new FRM_View_Ext_Form();
			
			foreach ($this->fields as $v){
				if(isset($v['name'])){
					if(!empty($this->localizedFields[$v['name']])){
						foreach($this->langs as $k=>$lang){
							$vv = $v;
							$part  = isset($v['part'])?$v['part']:'';
							if($part){
								$vv['name'] ="{$part}[{$k}][{$this->localizedFields[$v['name']]}]";
							}else{
								$vv['name'] ="{$k}[{$this->localizedFields[$v['name']]}]";
							}
							$vv['zone'] = $k;
							$vv['map'] ="{$this->localizedFields[$v['name']]}.$k";
							$vv['lang'] =$k;
							// Назначаем поля
							$form->addItem($vv);
						}
					}elseif (isset($v['search']) && $v['search']===true){
						// Назначаем поля
						$searchForm->addItem($v);
						//$form->addReader(array('name'=>$v['name']));
					}else{
						$part  = isset($v['part'])?$v['part']:'';
						$vv = $v;
						if($part){
							$vv['name'] ="{$part}[{$v['name']}]";
							$vv['map'] ="{$part}.{$v['name']}";
						}
						$form->addItem($vv);
						//$form->addReader(array('name'=>$v['name']));
					}
				}

			}



			//Назначаем название переменной в яваскріпте
			$form->SetVarName("editForm");

			$searchForm->SetVarName("searchForm");
			$searchForm->setLabelWidth(55);
			
			//Рендерим грид и передаем клиенту
			$component = $form->render($this->model->formTemplate);
			//$component = $form->render('SimpleDouble');

			$searchFormComponent = $searchForm->render("Search");

			//Создаем екземпляр окна
			$window = new FRM_View_Ext_Window();
			
			//Назначаем название переменной в яваскріпте
			$window->SetVarName("editWindow");


			$window->setWidth($this->windowFormWidth);

			$subPanel = new PhpExt_Panel();
			$subPanel->addItem($component);
			//$subPanel->setHeight(300);
			$subPanel->setAutoScroll(true);

			// Назначаем компоненты
			$window->AddItem($subPanel);

			//Рендерим окно
			$window->render();
			
			$items = 'grid';
			if(method_exists($this, 'items'.$method)){
				$method = 'items'.$method;
				$items = $this->$method();
			}
			
			//рендерим все
			return "<script type = 'text/javascript' >".$this->controller." = function() {
			".PhpExt_Ext::onLazyReady()."return {
      init : function() {
    
         }
      };
   }
();
Ext.onReady(".$this->controller.".init, ".$this->controller."); </script>";
		}
	}

	protected  function renderGrid($method){

		if(method_exists($this, $method)){
			$grid = $this->$method();
			$lang = null;

			if(method_exists($this->model, 'getActualLang')){
				$lang = $this->model->getActualLang($lang);
			}

			foreach ($this->fields as $v){
				if(!empty($v['grid']) && !empty($v['name']) && !empty($v['label'])){
					$grid->addItem($v);
				}
			}
			$grid->setCurrentLang($lang);
			$grid->render();
		}
	}

	protected  function grid(){
		$grid = new FRM_View_Ext_Grid();
		if ($this->model->getTreeFieldName()){
			$grid->setParentField($this->model->getTreeFieldName());
		}
		return $grid;
	}
	
	protected  function expandGrid(){
		$expandGrid = new FRM_View_Ext_ExpandGrid();
		$expandGrid -> setItemTpl($this->model->getItemTpl());
		return $expandGrid;
	}
	
	protected  function itemsGrid(){
		return "layout : 'fit', items : grid";

	}
	
	protected  function tree(){
		$this->renderGrid('grid');
		$tree = new FRM_View_Ext_Tree();
		$tree->setUrl('/'.$this->module.'/'.$this->model->getTreeControllerName().'/treedata/');
		$tree->setParentField($this->model->getTreeFieldName());
		return $tree;

	}
	
	protected  function itemsTree(){
		return "layout : 'border', items : [tree, new Ext.Panel( {
                  title : 'Элементы Меню', region : 'center', margins : '3 0 3 0', layout : 'fit', items : grid})]";
	}
	
	protected  function customGrid(){
		$grid = new FRM_View_Ext_CustomGrid();
		$grid ->setItemTpl($this->model->getItemTpl());
		$grid->setFields($this->model->getFields());
		return $grid;
	}
	
	
	protected  function itemsCustomGrid(){
		return "layout : 'fit', items : [searchForm, panel]";

	}

}